Python Interview Questions and Answers
Question: How do I share global variables across modules?Answer:The canonical way to share information across modules within a single program is to create a special module (often called config or cfg). Just import the config module in all modules of your application; the module then becomes available as a global name. Because there is only one instance of each module, any changes made to the module object get reflected everywhere.
config.py: mod.py: main.py: |
Most helpful rated by users:
- What is Python?
- How do I share global variables across modules?
- How do you set a global variable in a function in Python?
- Why can\'t I use an assignment in an expression?
- What are the rules for local and global variables in Python?